﻿using Dapper;
using Domain.Sys;
using Infrastructure.Dapper;
using IRepository.Sys;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Repository.Sys
{
    public class ModuleRepository : RepositoryBase<Module>, IModuleRepository
    {
        public async Task<List<Module>> GetByParentIdAsync(Module item)
        {
            var sql = new StringBuilder(@"SELECT * FROM Module WHERE IsDelete = 0 AND ISNULL(ParentId, '') = @ParentId");
            var param = new DynamicParameters();
            param.Add("@ParentId", item.ParentId);
            if (!string.IsNullOrWhiteSpace(item.Type))
            {
                sql.Append(" AND Type = @Type");
                param.Add("@Type", item.Type);
            }
            if (!string.IsNullOrWhiteSpace(item.Status))
            {
                sql.Append(" AND Status = @Status");
                param.Add("@Status", item.Status);
            }
            sql.Append(" ORDER BY Sort, CreateTime");
            using (IDbConnection conn = ConnectionFactory.CreateSqlConnection())
            {
                var result = await conn.QueryAsync<Module>(sql.ToString(), param);
                return result.ToList();
            }
        }

        public List<Module> GetByParentId(Module item)
        {
            var sql = new StringBuilder(@"SELECT * FROM Module WHERE IsDelete = 0 AND ISNULL(ParentId, '') = @ParentId");
            var param = new DynamicParameters();
            param.Add("@ParentId", item.ParentId);
            if (!string.IsNullOrWhiteSpace(item.Type))
            {
                sql.Append(" AND Type = @Type");
                param.Add("@Type", item.Type);
            }
            if (!string.IsNullOrWhiteSpace(item.Status))
            {
                sql.Append(" AND Status = @Status");
                param.Add("@Status", item.Status);
            }
            sql.Append(" ORDER BY Sort, CreateTime");
            using (IDbConnection conn = ConnectionFactory.CreateSqlConnection())
            {
                var result = conn.Query<Module>(sql.ToString(), param);
                return result.ToList();
            }
        }

        public async Task<List<string>> GetUserModule(string userId)
        {
            var sql = @"SELECT DISTINCT m.Id FROM dbo.AdminUser u 
                          LEFT JOIN dbo.UserRole ur ON u.Id = ur.UserId
                          LEFT JOIN dbo.RoleModule rm ON rm.RoleId = ur.RoleId
                          LEFT JOIN dbo.Module m ON rm.ModuleId = m.Id
                          WHERE u.Id = @UserId AND m.IsDelete = 0 AND m.Status = 0";

            using (IDbConnection conn = ConnectionFactory.CreateSqlConnection())
            {
                var result = await conn.QueryAsync<string>(sql, new { UserId = userId });
                return result.ToList();
            }
        }

        public bool CheckRoleControllerAction(string userId, string pageUrl)
        {
            var sql = @"SELECT COUNT(1) Num FROM dbo.AdminUser u 
                          LEFT JOIN dbo.UserRole ur ON u.Id = ur.UserId
                          LEFT JOIN dbo.RoleModule rm ON rm.RoleId = ur.RoleId
                          LEFT JOIN dbo.Module m ON rm.ModuleId = m.Id
                          WHERE u.Id = @UserId AND m.IsDelete = 0 AND m.Status = 0 AND m.PageUrl Like @PageUrl";

            using (IDbConnection conn = ConnectionFactory.CreateSqlConnection())
            {
                var result = conn.QueryFirstOrDefault<int>(sql, new { UserId = userId, PageUrl = $"%{pageUrl}%" });
                return result > 0;
            }
        }
    }
}
